package com.zdkj.subway.st.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zdkj.subway.st.entity.SwStOutputDetail;
import com.zdkj.subway.st.entity.SwStTicketBorrowReturn;
import com.zdkj.subway.st.entity.SwStTicketDestroy;
import com.zdkj.subway.st.entity.SwStTicketReceive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ProjectName: zdkj
 * @Package: com.zdkj.subway.st.service.impl
 * @ClassName: SwTkStockReceive
 * @Description:
 * @Author: kangjian
 * @CreateDate: 2020/7/12 18:02
 * @Version: 1.0
 */
@Service
public class SwTkOutStockReceive {
    @Autowired
    private SwStTicketReceiveServiceImpl swStTicketReceiveService;

    @Autowired
    private SwStTicketDestroyServiceImpl swStTicketDestroyService;

    @Autowired
    private SwStTicketBorrowReturnServiceImpl swStTicketBorrowReturnService;

    //该部分数据全部来自于 sys_dict_item type=inventory_out_type的类型数据
    public final String STOCK_RECEIVE="2";//领用出库
    public final String STOCK_DESTROY="5";//销毁出库
    public final String STOCK_BORROW="3";//借用出库

    /**
     * 车票出库新增时同步新增的3种出库类型
     * @param swStOutputDetail
     * @return
     * @throws Exception
     */
    public boolean saveTicketReceiveInfo(SwStOutputDetail swStOutputDetail) throws Exception{

        if(swStOutputDetail.getOutGoingType().equals(STOCK_RECEIVE))
        {
            return swStTicketReceiveService.save(swStOutputDetail);
        }else if (swStOutputDetail.getOutGoingType().equals(STOCK_DESTROY)){
            return swStTicketDestroyService.save(swStOutputDetail);
        }else if(swStOutputDetail.getOutGoingType().equals(STOCK_BORROW)){
            return swStTicketBorrowReturnService.borrowsave(swStOutputDetail);
        }
        return false;
    }

    /**
     * 车票出库删除时同步删除的3种出库类型
     * @param swStOutputDetail
     * @return
     */
    public boolean delTicketReceiveInfo(SwStOutputDetail swStOutputDetail) {
        //出库信息id
        Integer id = swStOutputDetail.getId();
        //符合出库类型的关联删除
        if(swStOutputDetail.getOutGoingType().equals(STOCK_RECEIVE))
        {
            return swStTicketReceiveService.remove(Wrappers.<SwStTicketReceive>lambdaQuery().eq(SwStTicketReceive::getOutputStockid,id));
        }else if (swStOutputDetail.getOutGoingType().equals(STOCK_DESTROY)){
            return swStTicketDestroyService.remove(Wrappers.<SwStTicketDestroy>lambdaQuery().eq(SwStTicketDestroy::getOutputStockid,id));
        }else if(swStOutputDetail.getOutGoingType().equals(STOCK_BORROW)){
            return swStTicketBorrowReturnService.remove(Wrappers.<SwStTicketBorrowReturn>lambdaQuery().eq(SwStTicketBorrowReturn::getOutputStockid,id));
        }
        return false;
    }
    public boolean updateTicketReceiveInfo(SwStOutputDetail swStOutputDetail){

        if(swStOutputDetail.getOutGoingType().equals(STOCK_RECEIVE)){
            SwStTicketReceive swStTicketReceive = new SwStTicketReceive();
            swStTicketReceive.setReceiveCardType(swStOutputDetail.getTicketType());
            swStTicketReceive.setActiveTime(swStOutputDetail.getActiveTime());
            swStTicketReceive.setTicketNo(swStOutputDetail.getTicketNo());
            swStTicketReceive.setReceiveNumber(swStOutputDetail.getTicketNumber());
            swStTicketReceive.setStockArea(swStOutputDetail.getStockArea());
            swStTicketReceive.setOutStorageTime(swStOutputDetail.getOutGoingTime());
            swStTicketReceive.setIsReceive("否");
            swStTicketReceive.setTicketName(swStOutputDetail.getTicketName());
            swStTicketReceive.setTicketPrice(swStOutputDetail.getTicketPrice());
            swStTicketReceive.setTicketTimes(swStOutputDetail.getTicketTimes());
            swStTicketReceive.setLatestStartTime(swStOutputDetail.getLatestStartTime());
            LambdaQueryWrapper<SwStTicketReceive> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SwStTicketReceive::getOutputStockid,swStOutputDetail.getId());
            return swStTicketReceiveService.update(swStTicketReceive,wrapper);
        }else if (swStOutputDetail.getOutGoingType().equals(STOCK_DESTROY)){
            SwStTicketDestroy swStTicketDestroy=new SwStTicketDestroy();
            swStTicketDestroy.setOutStorageTime(swStOutputDetail.getOutGoingTime());
            swStTicketDestroy.setDestroyCardType(swStOutputDetail.getTicketType());
            swStTicketDestroy.setActiveTime(swStOutputDetail.getActiveTime());
            swStTicketDestroy.setTicketNo(swStOutputDetail.getTicketNo());
            swStTicketDestroy.setDestroyNumber(swStOutputDetail.getTicketNumber());
            swStTicketDestroy.setStockArea(swStOutputDetail.getStockArea());
            swStTicketDestroy.setIsReceive("否");
            swStTicketDestroy.setTicketName(swStOutputDetail.getTicketName());
            swStTicketDestroy.setTicketPrice(swStOutputDetail.getTicketPrice());
            swStTicketDestroy.setTicketTimes(swStOutputDetail.getTicketTimes());
            swStTicketDestroy.setLatestStartTime(swStOutputDetail.getLatestStartTime());
            LambdaQueryWrapper<SwStTicketDestroy> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SwStTicketDestroy::getOutputStockid,swStOutputDetail.getId());
            return swStTicketDestroyService.update(swStTicketDestroy,wrapper);
        }else if(swStOutputDetail.getOutGoingType().equals(STOCK_BORROW)){
            SwStTicketBorrowReturn swStTicketBorrowReturn=new SwStTicketBorrowReturn();
            swStTicketBorrowReturn.setOutputStockTime(swStOutputDetail.getOutGoingTime());
            swStTicketBorrowReturn.setTicketType(swStOutputDetail.getTicketType());
            swStTicketBorrowReturn.setActiveTime(swStOutputDetail.getActiveTime());
            swStTicketBorrowReturn.setTicketNo(swStOutputDetail.getTicketNo());
            swStTicketBorrowReturn.setBorrowNumber(swStOutputDetail.getTicketNumber());
            swStTicketBorrowReturn.setStockArea(swStOutputDetail.getStockArea());
            swStTicketBorrowReturn.setBorrowTime(swStOutputDetail.getOutGoingTime());
            swStTicketBorrowReturn.setIsReceive("否");
            swStTicketBorrowReturn.setIsReturn("否");
            swStTicketBorrowReturn.setTicketName(swStOutputDetail.getTicketName());
            swStTicketBorrowReturn.setTicketPrice(swStOutputDetail.getTicketPrice());
            swStTicketBorrowReturn.setTicketTimes(swStOutputDetail.getTicketTimes());
            swStTicketBorrowReturn.setTicketType(swStOutputDetail.getTicketType());
            LambdaQueryWrapper<SwStTicketBorrowReturn> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SwStTicketBorrowReturn::getOutputStockid,swStOutputDetail.getId());
            return swStTicketBorrowReturnService.update(swStTicketBorrowReturn,wrapper);
        }
        return false;
    }

}
